package com.example.aftersales.service.impl;
import com.example.aftersales.service.AnnouncementService;
import com.example.aftersales.utils.Result;
import com.example.aftersales.entity.Announcement;
import com.example.aftersales.mapper.AnnouncementMapper;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class AnnouncementServiceImpl implements AnnouncementService {

    private final AnnouncementMapper announcementMapper;

    public AnnouncementServiceImpl(AnnouncementMapper announcementMapper) {
        this.announcementMapper = announcementMapper;
    }

    @Override
    public Result<List<Announcement>> getAllAnnouncements() {
        List<Announcement> announcements = announcementMapper.findAll();
        return Result.success("获取公告列表成功", announcements);
    }

    @Override
    public Result<Announcement> getAnnouncementById(Long id) {
        Announcement announcement = announcementMapper.findById(id);
        if (announcement != null) {
            return Result.success("获取公告成功", announcement);
        } else {
            return Result.error("未找到该公告");
        }
    }

    @Override
    public Result<String> addAnnouncement(Announcement announcement) {
        announcement.setPublishDate(LocalDate.now());
        int result = announcementMapper.insert(announcement);
        if (result > 0) {
            return Result.success("公告发布成功！");
        } else {
            return Result.error("公告发布失败，请稍后再试！");
        }
    }

    @Override
    public Result<String> updateAnnouncement(Announcement announcement) {
        int result = announcementMapper.update(announcement);
        if (result > 0) {
            return Result.success("公告编辑成功！");
        } else {
            return Result.error("公告编辑失败，请稍后再试！");
        }
    }

    @Override
    public Result<String> deleteAnnouncement(Long id) {
        int result = announcementMapper.deleteById(id);
        if (result > 0) {
            return Result.success("公告删除成功！");
        } else {
            return Result.error("公告删除失败，请稍后再试！");
        }
    }
}}